package me.aartikov.alligator.commands;


import me.aartikov.alligator.OhosNavigator;
import me.aartikov.alligator.NavigationContext;
import me.aartikov.alligator.animations.AnimationData;
import me.aartikov.alligator.exceptions.NavigationException;
import me.aartikov.alligator.Screen;
import me.aartikov.alligator.ScreenResult;

/**
 * Command implementation for {@code goBack} method of {@link OhosNavigator}.
 */
public class BackCommand implements Command {

    private ScreenResult mScreenResult;

    private AnimationData mAnimationData;
    private Screen mScreen;

    public BackCommand(Screen screen, ScreenResult screenResult, AnimationData animationData) {
        mScreenResult = screenResult;
        mAnimationData = animationData;
        mScreen = screen;
    }

    @Override
    public boolean execute(NavigationContext navigationContext) throws NavigationException {
        if (navigationContext.getDialogFragmentNavigator().canGoBack()) {
            navigationContext.getDialogFragmentNavigator().goBack(mScreenResult);
            return true;
        } else if (navigationContext.getFragmentNavigator() != null && navigationContext.getFragmentNavigator().canGoBack()) {
            return navigationContext.getFragmentNavigator().goBack(mScreen,mScreenResult, mAnimationData);
        } else if (navigationContext.getFlowFragmentNavigator() != null && navigationContext.getFlowFragmentNavigator().canGoBack()) {
            return navigationContext.getFlowFragmentNavigator().goBack(mScreen,mScreenResult, mAnimationData);
        } else {
            return navigationContext.getActivityNavigator().goBack(mScreenResult, mAnimationData);
        }
    }
}
